﻿using Abp.Domain.Uow;
using Abp.Runtime.Caching;
using HCD.Const;
using HCD.Web.Authorizer;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System.Linq;

namespace HCD.Web
{
    /// <summary>
    /// 控制器基类
    /// </summary>
    [UnitOfWork(IsDisabled = true)]
    public abstract class EnterpriseControllerBase : BaseCoreController
    {
        /// <summary>
        /// redis 缓存服务
        /// </summary>
        public ICacheManager CacheManager { get; set; }
        /// <summary>
        /// 读取appsetting.json配置
        /// </summary>
        public IConfiguration Configuration { get; set; }

        /// <summary>
        /// construction
        /// </summary>
        protected EnterpriseControllerBase()
        {
            LocalizationSourceName = EnterpriseConsts.LocalizationSourceName;
        }

        /// <summary>
        /// 当前用户信息
        /// </summary>
        protected AuthModel CurrentOperator
        {
            get
            {
                return RouteData.Values.Where(a => a.Key == "auth").FirstOrDefault().Value as AuthModel;
            }
        }
        
        protected string GetUserIp(HttpContext context)
        {
            var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrWhiteSpace(ip))
            {
                ip = context.Connection.RemoteIpAddress.ToString();
            }
            return ip;
        }
    }
}